package com.zhdl.network.asclltcp;

import com.project.command.faq.event.behavior.BehaviorPerform;
import com.project.command.instruction.bo.SingleInstruction;
import com.zhdl.common.util.IdUtil;
import com.zhdl.network.service.conf.PortConf;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.project.command.instruction.strategy.abs.AbsDic.INSTRUCTION_DEFAULT_HEX;

@Slf4j
@Service
public class AsciiSendService {
    @Resource
    private BehaviorPerform behaviorPerform;

    @Resource
    private PortConf portConf;
    private void sendCmd(String cmd){
        char[] charArray = cmd.toCharArray();
        StringBuffer stringBuffer = new StringBuffer();
        for (char c : charArray) {
            stringBuffer.append(Integer.toHexString(c));
        }
        String hexStr = stringBuffer.toString();
        log.info(hexStr);
        SingleInstruction instruction = new SingleInstruction();
        instruction.setInstructionId(IdUtil.getUUID_8());

        //设置要发送的指令字符串
        //转换成十进制
        instruction.setInstruction(hexStr);

        //INSTRUCTION_DEFAULT_HEX 原始字符串字符串
        //INSTRUCTION_DEFAULT 进行转码后发送执行
        instruction.setBeforeInstructionName(INSTRUCTION_DEFAULT_HEX);
        //默认使用com.zhdl.instruction.strategy包下后置策略
        instruction.setAfterInstructionName(AsciiAfterService.ASCLL_TCP_AFTER_SERVICE);
        //客户端通信地址, 串口为数字, 网口为ip+端口       数据来源 : port_comm 或 port_socket 表
//        instruction.setPortKey("3");
        //东区的电机串口待定 -- 可能是3 测试的时候为了避免冲突 设为 1
//        instruction.setPortKey("192.168.2.55:50000");
        instruction.setPortKey("127.0.0.1:50000");
        //设置协议  数据来源 : protocols 表
        instruction.setProtocols(AsciiTcpAnalysisImpl.ASCLL_TCP_ANALYSIS);
        behaviorPerform.perform(instruction);
    }
    public void sendLocationOrigin(){
        String cmd = "/f/bIII35III201III147IIIMoveJ(7.974,-128.304,121.538,-173.234,-97.974,-135.000," +
                "-150,-110,490,90,-45,-90,0,0,100,180,50," +
                "0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
        sendCmd(cmd);
    }

    public void sendLocationMove1(){
        String cmd = "/f/bIII37III201III147IIIMoveJ(1.773,-69.621,83.492,-173.477,-97.560,-136.201,-" +
                "456,-103,406,116,-38,-113,0,0,100,180,50," +
                "0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
        sendCmd(cmd);
    }

    public void sendLocationMove2(){
        String cmd = "/f/bIII38III201III170IIIMoveJ(89.618,-74.578,64.456,-127.747,-85.449,-135.835," +
                "106.911,-453.798,460.871,143.496,-33.703,-31.118,0,0,100,180,50," +
                "0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
        sendCmd(cmd);
    }

    public void sendLocationMove3(){
        String cmd ="/f/bIII9III201III149IIIMoveJ(4.339,-103.969,116.674,-173.696,-96.294,-135.042," +
                "-286,-113,423,115,-40,-109,0,0,100,180,50," +
                "0.000000,0.000000,0.000000,0.000000,0,0,0,0,0,0,0,0)III/b/f";
        sendCmd(cmd);
    }
    public void sendMeichStart(int reTry){
        String cmd = "p,1,"+reTry;
        sendCmd(cmd);
    }
}
